package com.bjshiwei.zhengu.ui.fragment;


import android.content.Intent;
import android.support.v4.app.Fragment;
import android.text.format.DateUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CheckBox;
import android.widget.ListView;

import com.bjshiwei.zhengu.R;
import com.bjshiwei.zhengu.adapter.AmplitudeAdapter;
import com.bjshiwei.zhengu.adapter.MyBaseAdapter;
import com.bjshiwei.zhengu.bean.SwingBean;
import com.bjshiwei.zhengu.config.NetConfig;
import com.bjshiwei.zhengu.requestInfo.RequestErrorInfo;
import com.bjshiwei.zhengu.requestInfo.RequestListenerInfo;
import com.bjshiwei.zhengu.utils.VolleyRequest;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class AmplitudeFragment extends BaseFragment {
    private List<SwingBean> mListBeen;
    private PullToRefreshListView mListView;
    private MyBaseAdapter mMyBaseAdapter;
    private VolleyRequest mVolleyRequest;
    private JSONObject mJsonRequset;
    private CheckBox mCheckBox;
    public boolean isCheck;
    private DecimalFormat df;
    private Intent mIntent;

    @Override
    protected void setListener() {
        mListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() {
            @Override
            public void onRefresh(PullToRefreshBase<ListView> refreshView) {
                getData();
            }
        });

       /* mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                mIntent = new Intent(getActivity(), MarketDetailActivity.class);
                String stockId = mListBeen.get(position-1).getId();
                String name = mListBeen.get(position-1).getName();
                mIntent.putExtra("id",stockId);
                mIntent.putExtra("name",name);
                startActivity(mIntent);
            }
        });*/
    }

    @Override
    protected void setData() {
        mListView.setAdapter(mMyBaseAdapter);
    }

    @Override
    protected void initData() {
        mListBeen = new ArrayList<>();
        mMyBaseAdapter = new AmplitudeAdapter(getContext(), mListBeen);
        mVolleyRequest = VolleyRequest.getInstance(getContext());
        df = new DecimalFormat("0.00");
        getData();
    }

    @Override
    protected View initView(LayoutInflater inflater) {
        return inflater.inflate(R.layout.fragment_amplitude, null);
    }

    @Override
    protected void initFindViewById(View view) {

        if (view != null) {
            mListView = (PullToRefreshListView) view.findViewById(R.id.lv_amplitude_body);
            mListView.setPullToRefreshOverScrollEnabled(true);
            mListView.getLoadingLayoutProxy().setPullLabel("���ظ���");
            mListView.getLoadingLayoutProxy().setRefreshingLabel("����ˢ��");
            mListView.getLoadingLayoutProxy().setReleaseLabel("�ͷſ�ʼˢ��");
            String label = DateUtils.formatDateTime(getActivity(), System.currentTimeMillis(),
                    DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_ALL);
            mListView.getLoadingLayoutProxy().setLastUpdatedLabel(label);
        }
    }

    public void getData() {
        mJsonRequset = mVolleyRequest.getJsonInstance();
        mVolleyRequest.jsonGetRequest(NetConfig.UPDOWNLIST_URL, mJsonRequset, new RequestListenerInfo(getActivity()) {

            @Override
            protected void onAnalysis(Object o, JSONObject jsonObject, boolean success) {
                try {

                    JSONObject UpDownListObject = new JSONObject(o.toString());

                    JSONArray jsonArray = UpDownListObject.getJSONArray("data");
                    JSONObject object = (JSONObject) jsonArray.get(2);
                    mListBeen.clear();
                    //�Ƿ������
                    JSONArray UpArray = object.getJSONArray("data");
                    for (int i = 0; i < UpArray.length(); i++) {
                        JSONObject json = (JSONObject) UpArray.get(i);
                        SwingBean swingBean = new SwingBean();
                        String name = json.getString("name");
                        String id = json.getString("code");
                        double price = json.getDouble("price");
                        double changeRate = json.getDouble("amplitude");

                        swingBean.setName(name);
                        swingBean.setId(id);
                        swingBean.setSwing(df.format(changeRate));
                        swingBean.setPrice(df.format(price));
                        mListBeen.add(swingBean);
                    }
                    mMyBaseAdapter.notifyDataSetChanged();

                    mListView.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            mListView.onRefreshComplete();
                        }
                    }, 500);

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new RequestErrorInfo() {
            @Override
            protected void onError() {

            }
        });
    }
}
